package com.wzz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzz.entity.CourseScore;
import com.wzz.entity.Exam;
import com.wzz.entity.ExamRecord;
import com.wzz.entity.Question;
import com.wzz.mapper.CourseScoreMapper;
import com.wzz.mapper.ExamMapper;
import com.wzz.mapper.ExamRecordMapper;
import com.wzz.mapper.QuestionMapper;
import com.wzz.vo.StatisticVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Resource
    CourseScoreMapper courseScoreMapper;

    @Resource
    QuestionMapper questionMapper;

    @Resource
    ExamMapper examMapper;

    @Resource
    ExamRecordMapper examRecordMapper;
    @Override
    public StatisticVO statistics(Integer courseId) {
        List<Exam> exams = examMapper.selectList(new LambdaQueryWrapper<Exam>().eq(Exam::getBankId, courseId));
        Set<Integer> examIds = exams.stream().map(Exam::getExamId).collect(Collectors.toSet());

        return StatisticVO.builder()
                .countOfStudent(courseScoreMapper.selectCount(new LambdaQueryWrapper<CourseScore>().eq(CourseScore::getCourseId, courseId)))
                .countOfQuestionBank(questionMapper.selectCount(new LambdaQueryWrapper<Question>().eq(Question::getBankId, courseId)))
                .countOfQuestionBankCreatedAtToday(questionMapper.selectCount(new LambdaQueryWrapper<Question>().eq(Question::getBankId, courseId).eq(Question::getCreateTime, LocalDate.now())))
                .timesOfExam(examRecordMapper.selectCount(new LambdaQueryWrapper<ExamRecord>().in(ExamRecord::getExamId, examIds)))
                .build();
    }
}
